package com.gitee.cirnochat.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.user.mapper.UserPrivacySettingMapper;
import com.gitee.cirnochat.user.model.entity.UserPrivacySetting;
import com.gitee.cirnochat.user.model.vo.req.user.UserPrivacySettingUpdateReq;
import com.gitee.cirnochat.user.model.vo.resp.UserPrivacySettingVo;
import com.gitee.cirnochat.user.service.UserPrivacySettingService;
import org.springframework.stereotype.Service;

/**
* @author FlanChan
* @description 针对表【user_privacy_setting(用户隐私设置表)】的数据库操作Service实现
* @createDate 2025-04-19 10:33:52
*/
@Service
public class UserPrivacySettingServiceImpl extends ServiceImpl<UserPrivacySettingMapper, UserPrivacySetting> implements UserPrivacySettingService {

    @Override
    public void resetPrivacySetting(Long userId) {
        this.update(new LambdaUpdateWrapper<>(UserPrivacySetting.class)
                .eq(UserPrivacySetting::getUserId, userId)
                .set(UserPrivacySetting::getSearchableByKeyword, true)
                .set(UserPrivacySetting::getSearchableByUuid, true));
    }

    @Override
    public void updatePrivacySetting(UserPrivacySettingUpdateReq req, Long userId) {
        this.update(new LambdaUpdateWrapper<>(UserPrivacySetting.class)
                .eq(UserPrivacySetting::getUserId, userId)
                .set(req.getSearchableByPhone() != null , UserPrivacySetting::getSearchableByKeyword, req.getSearchableByPhone())
                .set(req.getSearchableByUuid() != null,UserPrivacySetting::getSearchableByUuid, req.getSearchableByUuid()));

    }

    @Override
    public UserPrivacySettingVo getPrivacySettingByUserId(Long userId) {
        return this.lambdaQuery()
                .eq(UserPrivacySetting::getUserId, userId)
                .oneOpt()
                .map(UserPrivacySettingVo::new)
                .orElseGet(() -> {
                    UserPrivacySetting defaultSetting = new UserPrivacySetting();
                    defaultSetting.setUserId(userId);
                    defaultSetting.setSearchableByKeyword(true);
                    defaultSetting.setSearchableByUuid(true);
                    this.save(defaultSetting);
                    return new UserPrivacySettingVo(defaultSetting);
                });
    }
}




